home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
aminet
/
util
/
wb
/
wb_brushes.lha
/
Random
/
random.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-12-06
|
4KB
|
143 lines
;/* Random.c - Amiga Mail simple ExAll() example.
lc -cis -v -d0 -j73 -O Random.c
blink from lib:c.o Random.o to Random lib lib:lc.lib lib:amiga.lib ND SC SD
quit
*
* Simple program to choose a file randomly from a directory.
* Usage: Random <pattern>
* e.g. copy `random sys:prefs/presets/#?.wbpic` env:sys/wbpicture.prefs
*
* Written so that I can choose a random prefs-file for NickPrefs' picture,
* but could be used in all sorts of situations, I suppose.
*
* Note: Random numbers generated by looking at system time in seconds, and
* taking remainder modulo N (where N is number of files to choose from). This
* means that running Random many times quickly in succession will NOT generate
* a random sequence, but method is good enough for system boots etc.
*
* Martin W. Scott, 4 December 1992
*/
#include <exec/memory.h>
#include <exec/execbase.h>
#include <dos/dosextens.h>
#include <dos/rdargs.h>
#include <dos/exall.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <pragmas/exec_pragmas.h>
#include <pragmas/dos_pragmas.h>
#include <string.h>
/* Buffersize to receive filenames in */
#define BUFFERSIZE 4096
extern struct Library *DOSBase;
VOID _main(VOID);
BOOL ChooseRandom(char *, char *, char *);
extern struct ExecBase *SysBase;
VOID
_main(VOID)
{
struct RDArgs *readargs;
LONG rargs[2];
UBYTE *source;
/* Fail silently if < 37 */
if (SysBase->LibNode.lib_Version >= 37)
{
if (readargs = ReadArgs("PATTERN/A", rargs, NULL))
{
char fullpath[256], filename[50], pat[50], *t;
source = (UBYTE *) rargs[0];
strcpy(pat, FilePart(source));
if (t = PathPart(source))
*t = '\0';
if (ChooseRandom(source, pat, filename))
{
strcpy(fullpath, source);
AddPart(fullpath, filename, 256);
PutStr("\"");
PutStr(fullpath);
PutStr("\"");
}
FreeArgs(readargs);
}
else
PrintFault(IoErr(), NULL);
CloseLibrary((struct Library *) DOSBase);
}
}
/* seconds since midnight, jan 1, 1978 */
#define SECONDS(ds) (ds.ds_Days * 3600 * 24 + ds.ds_Minute * 60 + ds.ds_Tick / TICKS_PER_SECOND)
BOOL
ChooseRandom(char *source, char *pat, char *namebuf)
{
struct ExAllControl *excontrol;
struct ExAllData *ead, *buffer;
char *pattern, *parsebuffer;
LONG parselen;
BPTR sourcelock;
BOOL exmore;
BOOL success = FALSE;
if (buffer = AllocMem(BUFFERSIZE, MEMF_CLEAR))
{
if (sourcelock = Lock(source, SHARED_LOCK))
{
if (excontrol = AllocDosObject(DOS_EXALLCONTROL, NULL))
{
pattern = (!pat || !*pat) ? "#?" : pat;
if (parsebuffer = AllocVec(parselen = strlen(pattern) * 3, 0L))
{
/* Make pattern uppercase for possible character classes */
strupr(pattern);
if ((ParsePatternNoCase(pattern, parsebuffer, parselen)) != -1)
{
excontrol->eac_MatchString = parsebuffer;
excontrol->eac_LastKey = 0;
exmore = ExAll(sourcelock, buffer, BUFFERSIZE,
ED_NAME, excontrol);
if ((exmore == NULL && (IoErr() == ERROR_NO_MORE_ENTRIES)))
{
struct DateStamp ds;
UWORD idx, n;
if ((n = excontrol->eac_Entries) > 0)
{
ead = buffer;
DateStamp(&ds);
idx = SECONDS(ds) % n;
while (idx--)
ead = ead->ed_Next;
strcpy(namebuf, ead->ed_Name);
success = TRUE;
}
}
}
FreeVec(parsebuffer);
}
FreeDosObject(DOS_EXALLCONTROL, excontrol);
}
else PrintFault(ERROR_NO_FREE_STORE, NULL);
UnLock(sourcelock);
}
else PrintFault(IoErr(), source);
FreeMem(buffer, BUFFERSIZE);
}
else PrintFault(ERROR_NO_FREE_STORE, NULL);
return success;
}